package com.fan.shop.businesslogic;

import org.apache.log4j.Logger;

import com.fan.shop.domain.Stock;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.model.userclass.PurchaseLineUserClass;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.ItemService;
import com.fan.shop.services.PurchaseLineService;
import com.fan.shop.services.StockService;
import com.fan.shop.services.StoreService;

public class PurchaseOrderLineValideProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger(PurchaseOrderLineValideProcess.class);

	private StockService stockService;
	private PurchaseLineService purchaseLineService;
	private ItemService itemService;
	private StoreService storeService;
	private PurchaseLineUserClass purchaseLineUserClass;

	private String itemCode;
	private String storeCode;

	private String status;
	private String poNumber;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PurchaseOrderLineValideProcess.logger = logger;
	}

	public StockService getStockService() {
		return stockService;
	}

	public void setStockService(StockService stockService) {
		this.stockService = stockService;
	}

	public PurchaseLineService getPurchaseLineService() {
		return purchaseLineService;
	}

	public void setPurchaseLineService(PurchaseLineService purchaseLineService) {
		this.purchaseLineService = purchaseLineService;
	}

	public ItemService getItemService() {
		return itemService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public StoreService getStoreService() {
		return storeService;
	}

	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}

	public String getItemCode() {
		return itemCode;
	}

	public void setItemCode(String itemCode) {
		this.itemCode = itemCode;
	}

	public String getStoreCode() {
		return storeCode;
	}

	public void setStoreCode(String storeCode) {
		this.storeCode = storeCode;
	}

	public PurchaseLineUserClass getPurchaseLineUserClass() {
		return purchaseLineUserClass;
	}

	public void setPurchaseLineUserClass(
			PurchaseLineUserClass purchaseLineUserClass) {
		this.purchaseLineUserClass = purchaseLineUserClass;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getPoNumber() {
		return poNumber;
	}

	public void setPoNumber(String poNumber) {
		this.poNumber = poNumber;
	}

	@Override
	public Integer processImpl() {
		if (!itemService.hasItem(itemCode)) {
			setErrorMessage("Item Code: " + itemCode + " doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}
		if (!storeService.hasStore(storeCode)) {
			setErrorMessage("Store Code: " + storeCode + " doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}

		if (!stockService.hasStock(itemCode, storeCode)) {
			setErrorMessage("Stock: " + itemCode + " @" + storeCode
					+ " doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}

		Stock stock = stockService.getStock(itemCode, storeCode);

		int avaliableQty = stock.getQuantity();
		int holdQty = stock.getHoldQty();

		int requestedQty = purchaseLineUserClass.getQuantity();

		// TODO: edit or create
		int originQty = 0;
		if (status.equals(ShopGlobalVariables.ENTERING)) {
			if (purchaseLineService.getPurchaseLine(poNumber, itemCode) == null) {
				logger.info("Creating new Purchase Order");
			} else {
				originQty = purchaseLineService.getPurchaseLine(poNumber,
						itemCode).getQuantity();
			}
			if (requestedQty > (avaliableQty + originQty)) {
				setErrorMessage("Stock: " + itemCode + " @" + storeCode
						+ " doesn't have enough quantity: " + requestedQty);
				return ShopGlobalVariables.FAILED;
			}
		}
		return ShopGlobalVariables.SUCCESS;
	}

}
